package com.chencs.qiniu;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.IOException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;

public class UploadDemo {

  String ACCESS_KEY = "Di25z1Z3XQv-ssuLoxG4h9JHQVPOY0foKXufBlaX";
  String SECRET_KEY = "vQpxKblnBc4VuCOe_uRX1_e7rNt2ZdyUxdiugjto";
  String bucketname = "heitao";
  String key = "";
  byte[] newfile;

  public UploadDemo(final byte[] file,final String filename){
	  this.key = filename;
	  this.newfile = file;
	  
  }

  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
  Configuration cfg = new Configuration(Zone.zone2());
  UploadManager uploadManager = new UploadManager(cfg);
  public String getUpToken(){
      return auth.uploadToken(bucketname);
  }

  public String upload() throws IOException{
    try {
      Response res = uploadManager.put(newfile, key, getUpToken());

      JsonObject returnData = new JsonParser().parse(res.bodyString()).getAsJsonObject();
      return   res.bodyString(); //returnData.get("key").toString();
      } catch (QiniuException e) {
      e.printStackTrace();
            return "-1";
      }       
  }

  
  
  


}


